package com.tys.algorithm.beginner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Code19_ShowComparator {
    public static class Student {
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }
    }

    //从大到小排
    public static class MyComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1 < o2) {
                return 1;
            } else if (o1 > o2) {
                return -1;
            } else {
                return 0;
            }
        }
    }

    //Id比较器
    public static class IdComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.id < o2.id) { //大的在前面排
                return 1; //返回正，则第二个参数在前面排
            } else if (o2.id < o1.id) {
                return -1;//返回负，第一个参数在前面排
            } else {
                return 0;//返回0，哪个排前面无所谓
            }
        }
    }

    //age比较器
    public static class AgeComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            if (o1.age < o2.age) { //大的在前面排
                return 1; //返回正，则第二个参数在前面排
            } else if (o2.age < o1.age) {
                return -1;//返回负，第一个参数在前面排
            } else {
                return 0;//返回0，哪个排前面无所谓
            }
        }
    }

    //打印数组
    public static void printArray(Integer[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void printStudents(Student[] students) {
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].name + ", " + students[i].id + ", " + students[i].age);
        }
    }

    public static void printStudentsArrayList(ArrayList<Student> arrayList) {
        for (Student s : arrayList) {
            System.out.println(s.name + ", " + s.id + ", " + s.age);
        }
    }

    public static void main(String[] args) {
        Integer[] arr = {8, 1, 4, 1, 6, 8, 4, 1, 5, 8, 2, 3, 0};
        printArray(arr);
        System.out.println("=====默认从小到大排begin=====");
        Arrays.sort(arr);
        printArray(arr);
        System.out.println("=====从大到小排begin=====");
        Arrays.sort(arr, new MyComparator());
        printArray(arr);

        //自定义类排序
        Student s1 = new Student("张三", 5, 27);
        Student s2 = new Student("李四", 1, 17);
        Student s3 = new Student("王五", 4, 29);
        Student s4 = new Student("赵六", 3, 9);
        Student s5 = new Student("左七", 2, 34);

        Student[] students = {s1, s2, s3, s4, s5};
        printStudents(students);
        System.out.println("=====id从大到小排begin=====");
        Arrays.sort(students, new IdComparator());
        printStudents(students);
        System.out.println("=====id从大到小排end=====");

        ArrayList<Student> arrayList = new ArrayList<>();
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        printStudentsArrayList(arrayList);
        System.out.println("=====age从大到小排begin=====");
        arrayList.sort(new AgeComparator());
        printStudentsArrayList(arrayList);
        System.out.println("=====age从大到小排end=====");

    }
}
